﻿package mortal.game.view.mainUI.petAvatar
{
    import Message.Game.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.manager.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.model.vo.pet.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.button.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.mainUI.avatar.*;
    import mortal.mvc.core.*;

    public class PetAvatarView extends View
    {
        protected var _petAvatar:PetAvatar;
        protected var _outBtn:TimeButton;
        protected var _stateBtn:GLoadedButton;
        protected var _pet:SPet;
        protected var _toolTipSprite:ToolTipSprite;
        private var _lastState:int = 0;
        private var _lastFightMode:int = 0;
        private var _btnStyleName:String;
        private var _isInPetBreakCopy:Boolean;

        public function PetAvatarView()
        {
            layer = LayerManager.uiLayer;
            return;
        }// end function

        override protected function configUI() : void
        {
            super.configUI();
            this._petAvatar = new PetAvatar();
            this.addChild(this._petAvatar);
            this._outBtn = UIFactory.timeButton("", 9, 43, 17, 17, this, Cache.instance.cd.PET_REBACk, "PetHeadCallbackBtn");
            this._outBtn.isByClick = false;
            this._outBtn.cdTime = PetConst.REBACK_TIME;
            this._outBtn.type = TimeButton.CD;
            this._toolTipSprite = UICompomentPool.getUICompoment(ToolTipSprite);
            this._toolTipSprite.mouseEnabled = true;
            UIFactory.setObjAttri(this._toolTipSprite, 26, 43, -1, 100, this);
            this._toolTipSprite.toolTipData = "xxxxxxxxxxxxxx";
            this._stateBtn = UIFactory.gLoadedButton(ImagesConst.PetHeadFollow_upSkin, 0, 0, 17, 17, this._toolTipSprite);
            this._outBtn.addEventListener(MouseEvent.CLICK, this.onClickOutBtn);
            this._stateBtn.addEventListener(MouseEvent.CLICK, this.onClickStateBtn);
            return;
        }// end function

        protected function onClickOutBtn(event:MouseEvent) : void
        {
            var _loc_2:* = this._pet.publicPet.uid;
            var _loc_3:* = this._outBtn.styleName == "PetHeadOutBtn" || this._outBtn.styleName == "PetHeadReliveBtn" ? (EPetState._EPetStateActive) : (EPetState._EPetStateIdle);
            var _loc_4:* = new PetOutOrInVO(_loc_2, _loc_3);
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetOutOrIn, _loc_4));
            return;
        }// end function

        protected function onClickStateBtn(event:MouseEvent) : void
        {
            var _loc_2:* = this._pet.publicPet.uid;
            var _loc_3:* = EPetMode._EPetModeActive;
            switch(this._stateBtn.styleName)
            {
                case ImagesConst.PetHeadFollow_upSkin:
                {
                    _loc_3 = EPetMode._EPetModeDefense;
                    break;
                }
                case ImagesConst.PetHeadDefense_upSkin:
                {
                    _loc_3 = EPetMode._EPetModeActive;
                    break;
                }
                case ImagesConst.PetHeadAttack_upSkin:
                {
                    _loc_3 = EPetMode._EPetModeFollow;
                    break;
                }
                default:
                {
                    break;
                }
            }
            var _loc_4:* = new PetChangeModeVO(_loc_2, _loc_3);
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetChangeMode, _loc_4));
            return;
        }// end function

        public function updateEntityInfo(param1:EntityInfo) : void
        {
            this._petAvatar.updateEntity(param1);
            return;
        }// end function

        public function onPetDead() : void
        {
            this._outBtn.styleName = "PetHeadReliveBtn";
            this.filters = [FilterConst.colorFilter];
            return;
        }// end function

        public function updatePet(param1:SPet) : void
        {
            var _loc_2:Boolean = false;
            var _loc_3:ICDData = null;
            var _loc_4:Number = NaN;
            var _loc_5:int = 0;
            if (!this._pet || param1.publicPet.state != this._lastState)
            {
                switch(param1.publicPet.state)
                {
                    case EPetState._EPetStateActive:
                    {
                        this.filters = null;
                        this._btnStyleName = "PetHeadCallbackBtn";
                        if (this._pet)
                        {
                            _loc_2 = true;
                        }
                        break;
                    }
                    case EPetState._EPetStateIdle:
                    {
                        this._btnStyleName = "PetHeadOutBtn";
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            if (!this._pet || Cache.instance.pet.fightMode != this._lastFightMode)
            {
                switch(Cache.instance.pet.fightMode)
                {
                    case EPetMode._EPetModeActive:
                    {
                        this._stateBtn.styleName = ImagesConst.PetHeadAttack_upSkin;
                        this._toolTipSprite.toolTipData = Language.getString(70001);
                        break;
                    }
                    case EPetMode._EPetModeDefense:
                    {
                        this._stateBtn.styleName = ImagesConst.PetHeadDefense_upSkin;
                        this._toolTipSprite.toolTipData = Language.getString(70002);
                        break;
                    }
                    case EPetMode._EPetModeFollow:
                    {
                        this._stateBtn.styleName = ImagesConst.PetHeadFollow_upSkin;
                        this._toolTipSprite.toolTipData = Language.getString(70003);
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            this._pet = param1;
            this._lastState = param1.publicPet.state;
            this._lastFightMode = Cache.instance.pet.fightMode;
            if (this._pet)
            {
                if (this._outBtn)
                {
                    this._outBtn.dispose();
                    this._outBtn = null;
                }
                _loc_3 = Cache.instance.cd.registerCDData(CDDataType.timeButton, Cache.instance.cd.PET_REVIVE);
                _loc_4 = ClockManager.instance.nowDate.time - this._pet.publicPet.lastDeadDt.time;
                if (_loc_4 < PetConst.REVIVE_TIME)
                {
                    this._outBtn = UIFactory.timeButton("", 9, 43, 17, 17, null, Cache.instance.cd.PET_REVIVE, this._btnStyleName);
                    this._outBtn.type = TimeButton.CD;
                    this.addChild(this._outBtn);
                    this._outBtn.isByClick = false;
                    this._outBtn.cdTime = PetConst.REVIVE_TIME;
                    _loc_5 = _loc_4;
                    _loc_3.beginTime = getTimer() - _loc_5;
                    this._outBtn.startCoolDown();
                }
                else
                {
                    this._outBtn = UIFactory.timeButton("", 9, 43, 17, 17, null, Cache.instance.cd.PET_REBACk, this._btnStyleName);
                    this._outBtn.type = TimeButton.CD;
                    this.addChild(this._outBtn);
                    this._outBtn.isByClick = false;
                    this._outBtn.cdTime = PetConst.REBACK_TIME;
                    if (_loc_2)
                    {
                        this._outBtn.startCoolDown();
                    }
                }
            }
            return;
        }// end function

        public function refreshDrugNum() : void
        {
            this._petAvatar.refreshDrugNum();
            return;
        }// end function

        public function updateCd() : void
        {
            this._petAvatar.updateCDEffect();
            return;
        }// end function

        public function specialUpdate() : void
        {
            this._petAvatar.onPetLifeUpdate(this._pet);
            this._petAvatar.specialUpdateLevel(this._pet.publicPet.level);
            this._petAvatar.specialUpdateAvatar(this._pet.publicPet.code);
            return;
        }// end function

        public function isInPetBreakCopy(param1:Boolean) : void
        {
            this._isInPetBreakCopy = param1;
            this._outBtn.visible = !this._isInPetBreakCopy;
            this._stateBtn.visible = !this._isInPetBreakCopy;
            this.stageResize();
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            this.refreshDrugNum();
            return;
        }// end function

        public function setSliderValue() : void
        {
            this._petAvatar.setClientValue();
            return;
        }// end function

        override public function stageResize() : void
        {
            if (this._isInPetBreakCopy)
            {
                this.x = 0;
                this.y = 0;
            }
            else
            {
                this.x = 220;
                this.y = 110;
            }
            return;
        }// end function

    }
}
